package com.wl.studyjob.dbchange.hbase;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.CollectionUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class dropUserObserver extends BaseRegionObserver {

    @Test
    @Override
    public void postDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit, Durability durability) throws IOException {
        //获取表
        HTableInterface app_user = e.getEnvironment().getTable(TableName.valueOf("app_user"));

        //获取当前delete事件，所删除的所有好友
        List<Cell> friends = delete.getFamilyCellMap().get(Bytes.toBytes("friends"));
        if(CollectionUtils.isEmpty(friends)){
            app_user.close();
            return;
        }

        for (Cell friend : friends) {
            //
            Delete dropUser = new Delete(CellUtil.cloneQualifier(friend));
            dropUser.addColumn(CellUtil.cloneFamily(friend),CellUtil.cloneRow(friend));
            app_user.delete(dropUser);
        }

        app_user.close();

    }
}
